//
//  SunWebVC.m
//  Xspcf
//
//  Created by sunshine on 16/10/19.
//  Copyright © 2016年 小小算盘. All rights reserved.
//

#import "SunWebVC.h"
#import "SunLoginVC.h"
#import "SunSecondVC.h"
#import <UMSocialCore/UMSocialCore.h>
#import "SunProductDetailVC.h"

@interface SunWebVC ()<UIWebViewDelegate>
{
    UIWebView *_web;
}
@end

@implementation SunWebVC

- (void)viewDidLoad {
    [super viewDidLoad];
    _web = [[UIWebView alloc]init];
    [self.view addSubview:_web];
    [_web lt_All:SunEdgesMake(0, 0, 0, 0) save:YES];
    [_web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.urlStr]]];
    _web.scalesPageToFit = YES;
    _web.delegate = self;
    [[Sun shareSun]showActivity:self];
    if (![self.urlStr containsString:@"http"]) {
        self.urlStr = [KHTTP stringByAppendingPathComponent:self.urlStr];
    }
    if (self.share) {
        [self creatShare];
    }
    // Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}
- (void)creatShare{
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, X(50), Y(25))];
    [btn setImage:[UIImage imageNamed:@"share_btn"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(gotoInvteFriend) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barbutton = [[UIBarButtonItem alloc]initWithCustomView:btn];
    self.navigationItem.rightBarButtonItem = barbutton;
}

- (void)gotoInvteFriend{
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = WHITE;
    NSArray *arr = @[@{@"img":@"share_line",@"title":@"朋友圈"},@{@"img":@"share_friend",@"title":@"微信好友"},@{@"img":@"share_qq",@"title":@"QQ好友"},@{@"img":@"share_sina",@"title":@"新浪微博"}];
    for (int i=0; i<4; i++) {
        UIButton *btn = [[UIButton alloc]init];
        [view addSubview:btn];
        [btn lt_setWidth:SCREENWIDTH / 4 save:YES];
        [btn lt_setHeight:Y(82) save:YES];
        [btn lt_setTop:0 to:nil save:YES];
        [btn lt_setLeft:SCREENWIDTH / 4 * i to:nil save:YES];
        btn.tag = i + 1;
        [btn setImage:[UIImage imageNamed:[arr[i] objectForKey:@"img"]] forState:UIControlStateNormal];
        [btn setTitle:[arr[i] objectForKey:@"title"] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:12];
        [btn setTitleColor:BLACK forState:UIControlStateNormal];
        [btn verticalImageAndTitle:40];
        [btn addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside];
    }
    UIButton *btn = [[UIButton alloc]init];
    [view addSubview:btn];
    [btn lt_Top:SunEdgesMake(Y(86), X(15), X(15), Y(44)) to:nil save:YES];
    btn.clipsToBounds = YES;
    [btn setBackgroundColor:BLUE forState:UIControlStateNormal];
    [btn setBackgroundColor:[HXColor colorWithHexString:@"87CEFA"] forState:UIControlStateHighlighted];
    btn.layer.cornerRadius = 4;
    [btn setTitle:@"取消" forState:UIControlStateNormal];
    [btn setTitleColor:WHITE forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:17];
    [btn doclick:^{
        [[Sun shareSun]hideShow];
    }];
    [[Sun shareSun]show:view to:[UIApplication sharedApplication].keyWindow shouldReturn:NO];
    [view lt_Bottom:SunEdgesMake(0, 0, 0, Y(144)) to:nil save:YES];
}


- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    [[Sun shareSun]hideAll];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [[Sun shareSun]hideAll];
    if (self.titleName) {
        self.title = self.titleName;
    }else{
        self.title = [_web stringByEvaluatingJavaScriptFromString:@"document.title"];
    }
}

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSString *str = [NSString stringWithFormat:@"%@",request.URL];
    if ([[[[[str componentsSeparatedByString:@"/"] lastObject] componentsSeparatedByString:@"."] firstObject] isEqualToString:@"goToLogin"]) {
        SunLoginVC *vc = [[SunLoginVC alloc] init];
        vc.isWeb = YES;
        [self.navigationController pushViewController:vc animated:YES];
        return NO;
    }else if ([[[[[str componentsSeparatedByString:@"/"] lastObject] componentsSeparatedByString:@"."] firstObject] isEqualToString:@"goToProductList"]){
        self.tabBarController.selectedIndex = 1;
        [self.navigationController popViewControllerAnimated:YES];
        return NO;
    }else if ([[[[[str componentsSeparatedByString:@"/"] lastObject] componentsSeparatedByString:@"."] firstObject] isEqualToString:@"goToProductDetail"]){
        // webToProductDetail
        SunProductDetailVC *productDetail = [[SunProductDetailVC alloc]init];
        NSString *borrowId = [[str componentsSeparatedByString:@"="] lastObject];
        productDetail.borrowId = borrowId;
        productDetail.isWeb = YES;
        [self.navigationController pushViewController:productDetail animated:YES];
        return NO;
    }
    
    
    return YES;
}

- (BOOL)navigationShouldPopOnBackButton{
    if (_web.canGoBack) {
        [_web goBack];
        return NO;
    }
    return YES;
}

- (void)share:(UIButton *)btn{
    [[Sun shareSun]hideShow];
    UMSocialPlatformType type;
    switch (btn.tag) {
        case 1:
        type = UMSocialPlatformType_WechatTimeLine;
        break;
        case 2:
        type = UMSocialPlatformType_WechatSession;
        break;
        case 3:
        type = UMSocialPlatformType_QQ;
        break;
        case 4:
        type = UMSocialPlatformType_Sina;
        break;
        
        default:
        break;
    }
    NSString *img = [self.shareData objectForKey:@"shareImg"];
    if (![img containsString:@"http"]) {
        img = [KIMG stringByAppendingString:img];
    }
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:[self.shareData objectForKey:@"shareTitle"] descr:[self.shareData objectForKey:@"shareContent"] thumImage:img];
    shareObject.webpageUrl =[self.shareData objectForKey:@"shareUrl"];
    messageObject.shareObject = shareObject;
    [[UMSocialManager defaultManager] shareToPlatform:type messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
